﻿/*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * See product pages for more information.
 * http://closesudoku.codeplex.com
 * Copyright (C), 2010, hrondra
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Close.Classes
{

    /// <summary>
    ///A class prepared for logging. 
    /// </summary>
    class MyLog
    {
        static readonly object padlock = new object();

        /// <summary>
        /// Level of the information.
        /// </summary>
        public enum LogType { Message = 10, Warning = 20, Error = 30 };
        public static void Write(LogType type, String header, String message, Exception ex)
        {
            try
            {
                lock (padlock)
                {
                    string exceptionExtract;
                    if (ex != null)
                        exceptionExtract = ex.Message;
                    byte typeNumber = (byte)type;

                    if (typeNumber >= ThemeClass.Settings.Logging)
                    {
                        String full_path = System.Reflection.Assembly.GetCallingAssembly().GetName().CodeBase;
                        String directory_path = full_path.Substring(0, full_path.LastIndexOf("\\"));
                        String pth = Path.Combine(directory_path, "Settings\\log.txt");
                        using (StreamWriter outfile = new StreamWriter(pth, true))
                        {
                            outfile.WriteLine(String.Format("{0} - {1}; {2}", header, message, ex));
                        }
                    }
                }
            }
            catch
            {
            }
        }

        public static void Write(LogType type, String header, String message)
        {
            Write(type, header, message, null);
        }
    }
}
